import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrl = CommonConfig.miotUrl + '/v1'

/**
 * 新增任务
 * @param data
 */
export function createBoxTask (data) {
  const newUrl = baseUrl + '/boxtask/create'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 编辑任务
 * @param data
 */
export function updateBoxTask (data) {
  const newUrl = baseUrl + '/boxtask/update'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

export function getCreateUser () {
  const newUrl = baseUrl + '/boxtask/listOwner'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取盒子分页任务
 * @param params
 */
export function getBoxTask (params) {
  const newUrl = baseUrl + '/boxtask/page'

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取详情
 * @param id
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getTaskDetail (id, isUpdate = 0) {
  const newUrl = baseUrl + `/boxtask/detail/${id}?isUpdate=${isUpdate}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子重试
 * @param id
 * @returns {Promise<AxiosResponse<any>>}
 */
export function retryRestart (id) {
  const newUrl = baseUrl + `/boxtask/retryRestart/${id}`

  return axiosInstance.put(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 删除任务
 * @param id
 * @returns {Promise<AxiosResponse<any>>}
 */
export function removeTask (id) {
  const newUrl = baseUrl + `/boxtask/remove/${id}`

  return axiosInstance.put(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 关闭任务
 * @param id
 * @returns {Promise<AxiosResponse<any>>}
 */
export function closeTask (id) {
  const newUrl = baseUrl + `/boxtask/close/${id}`

  return axiosInstance.put(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取升级驱动版本
 */
export function getLocalDriverVersionList () {
  const newUrl = baseUrl + `/boxtask/driver/localDriverVersionList`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function getWaitBoxSize () {
  const newUrl = baseUrl + `/boxtask/waitBoxSize`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
